package com.liehu.user.service;

import com.liehu.user.dao.DetailCustomerMapper;
import com.liehu.user.pojo.DetailCustomer;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * DetailCustomer 客户信息详细表的service层
 */
@Service
@Transactional //事务
public class DetailCustomerService {
    @Autowired
    private DetailCustomerMapper detailCustomerMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据客户id查询
     */
    public DetailCustomer findByCustomerId(String customerId){
        return detailCustomerMapper.findByCustomerId(customerId);
    }

    /**
     * 更新
     */
    public void update(DetailCustomer detailCustomer){
        //先获取token
        Claims claims1 = (Claims) request.getAttribute("claims_customer");
        Claims claims2 = (Claims) request.getAttribute("claims_admin");
        if(claims1 != null || claims2!=null){
            detailCustomerMapper.updateById(detailCustomer);
        }else{
            throw new RuntimeException("权限不足！");
        }


    }


    /**
     * 插入
     */
    public void save(DetailCustomer detailCustomer){
        Claims claims1 = (Claims) request.getAttribute("claims_customer");
        if(claims1 != null ){
            throw new RuntimeException("权限不足！");
        }
        //获取ID
        String customerId = claims1.getId();
        detailCustomer.setCustomerId(customerId);
        detailCustomerMapper.insert(detailCustomer);
    }


    /**
     * 删除
     */
    public void delete(String customerId){
        Claims claims2 = (Claims) request.getAttribute("claims_admin");
        if(claims2 == null){
            throw new RuntimeException("权限不足！");
        }
        detailCustomerMapper.deleteById(customerId);
    }


}
